﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using Telerik.WinControls.UI.Export;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class ViewThongKeTaiLieuCungCap : Form
    {
        SaveFileDialog saveFileDialog;

        public ViewThongKeTaiLieuCungCap()
        {
            InitializeComponent();

            LoadDataForLoaiTaiLieu();
            LoadDataForDangTaiLieu();
            LoadDataForTenTaiLieu();
            LoadDataForMaTaiLieu();
            LoadDataForNguoiNhan();
            LoadDataForDonVi();
            LoadDataForNam();

            GridViewSummaryItem summaryThanhTien = new GridViewSummaryItem("ThanhTien", "Tổng = {0: #,#}", GridAggregateFunction.Sum);
            GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem(new GridViewSummaryItem[] { summaryThanhTien });
            this.radGridView1.SummaryRowsTop.Add(summaryRowItem);
        }

        private void SetPreferences()
        {
            radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.ShowGroupPanel = false;
            radGridView1.MasterTemplate.EnableGrouping = false;
            radGridView1.EnableHotTracking = true;
            radGridView1.CommandCellClick += ViewDetail;
        }

        void ViewDetail(object sender, EventArgs e)
        {
            int id = Convert.ToInt32((sender as GridCommandCellElement).Value);
            Utilities.CloseForm(this, new DisplayBangKeKhaiPhiSuDung(id));
        }

        private void BuildData()
        {
            radGridView1.MasterTemplate.AllowAddNewRow = false;
            radGridView1.MasterTemplate.AutoGenerateColumns = false;
            
            string nguoiNhan = Convert.ToString(drlNguoiNhan.SelectedItem);
            string donVi = Convert.ToString(drlDonVi.SelectedItem);

            int? nam = null;
            if (!string.IsNullOrEmpty(Convert.ToString(drlNam.SelectedItem)))
            {
                nam = Convert.ToInt32(drlNam.SelectedItem);
            }             

            string loaiTaiLieu = Convert.ToString(drlLoaiTaiLieu.SelectedItem);
            string dangTaiLieu = Convert.ToString(drlDangTaiLieu.SelectedItem);
            string tenTaiLieu = Convert.ToString(drlTenTaiLieu.SelectedItem);
            string maTaiLieu = Convert.ToString(drlMaTaiLieu.SelectedItem);

            var phieuBiz = new PhieuBanGiaoTaiLieuBiz();
            var ids = phieuBiz.GetByParam(nguoiNhan, donVi, nam);

            var chiTietBiz = new PhieuBanGiaoTaiLieuChiTietBiz();
            var data = chiTietBiz.LoadItemsByIsAndParam(ids, loaiTaiLieu, dangTaiLieu, tenTaiLieu, maTaiLieu);
            if (data != null && data.Count > 0)
            {
                radGridView1.MasterTemplate.DataSource = data;
            }
            else
            {
                radGridView1.MasterTemplate.DataSource = null;                                
            }
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                //Utilities.ResetBackColor(parent.TopMenu, Constants.MenuIndex.TaiLieuVanBanPhapLy);
                parent.Status.Text = Constants.FormStatus.ThongKeTaiLieuCungCap;
            }

            SetPreferences();            

            CheckSecurity();
        }

        private void CheckSecurity()
        {
            var parrent = MdiParent as MDI;

            if (parrent == null) return;

            //if (!string.Equals(Constants.Securities.QuanLyVanBanPhapLyVaKeHoach, parrent.CurrentUser.QuyenTruyCap, StringComparison.InvariantCultureIgnoreCase)
            //    && !string.Equals(Constants.Securities.Administrator, parrent.CurrentUser.QuyenTruyCap, StringComparison.InvariantCultureIgnoreCase))
            //{
            //    buttonEdit.Visible = false;
            //    buttonRemove.Visible = false;
            //    radButton2.Visible = false;
            //}
        }

        private void Print(object sender, EventArgs e)
        {
            saveFileDialog = new SaveFileDialog { Filter = "Excel (*.xlsx)|*.xlsx" };

            if (saveFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (saveFileDialog.FileName.Equals(String.Empty))
            {
                RadMessageBox.SetThemeName(this.radGridView1.ThemeName);
                RadMessageBox.Show("Hãy nhập tên file báo cáo!");
                return;
            }

            string fileName = this.saveFileDialog.FileName;

            var file = new FileInfo(fileName);

            if (file.Extension.Equals(".xlsx"))
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                RunExportToExcelML(fileName);
            }
            else
            {
                RadMessageBox.SetThemeName(this.radGridView1.ThemeName);
                RadMessageBox.Show("Sai kiểu báo cáo!");
            }
        }

        private void RunExportToExcelML(string fileName)
        {
            var excelExporter = new ExportToExcelML(this.radGridView1);

            //set exporting visual settings
            excelExporter.ExportVisualSettings = true;

            try
            {
                foreach (var row in radGridView1.Rows)
                {
                    row.IsVisible = false;
                }

                foreach (var childRow in radGridView1.ChildRows)
                {
                    childRow.IsVisible = true;
                }

                //radGridView1.Rows

                excelExporter.SummariesExportOption = SummariesOption.ExportAll;
                excelExporter.HiddenRowOption = HiddenOption.DoNotExport;
                excelExporter.HiddenColumnOption = HiddenOption.DoNotExport;
                excelExporter.RunExport(fileName);

                foreach (var row in radGridView1.Rows)
                {
                    row.IsVisible = true;
                }

                RadMessageBox.SetThemeName(radGridView1.ThemeName);
                RadMessageBox.Show("Tạo báo cáo thành công.", "Báo cáo");
            }
            catch (IOException ex)
            {
                RadMessageBox.SetThemeName(this.radGridView1.ThemeName);
                RadMessageBox.Show(this, ex.Message, "I/O Error", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }

        private void LoadDataForLoaiTaiLieu()
        {
            List<string> data = new List<string>();

            var thamKhaoBiz = new ThamKhaoTaiLieuBiz();
            data.AddRange(thamKhaoBiz.GetAllLoaiTaiLieu());

            var khaiThacBiz = new KhaiThacVaSuDungTaiLieuBiz();
            data.AddRange(khaiThacBiz.GetAllLevel1());

            data.Sort();
            data.Insert(0, string.Empty);

            drlLoaiTaiLieu.DataSource = data;
        }

        private void LoadDataForDangTaiLieu()
        {
            List<string> data = new List<string>();
           
            var khaiThacBiz = new KhaiThacVaSuDungTaiLieuBiz();
            data.AddRange(khaiThacBiz.GetAllLevel2());
            data.Sort();
            data.Insert(0, string.Empty);

            drlDangTaiLieu.DataSource = data;
        }

        private void LoadDataForTenTaiLieu()
        {
            List<string> data = new List<string>();

            var khaiThacBiz = new KhaiThacVaSuDungTaiLieuBiz();
            data.AddRange(khaiThacBiz.GetAllTenTaiLieu());
            data.Sort();
            data.Insert(0, string.Empty);

            drlTenTaiLieu.DataSource = data;
        }

        private void LoadDataForMaTaiLieu()
        {
            List<string> data = new List<string>();

            var thamKhaoBiz = new ThamKhaoTaiLieuBiz();
            data.AddRange(thamKhaoBiz.GetAllMaTaiLieu());

            var khaiThacBiz = new KhaiThacVaSuDungTaiLieuBiz();
            data.AddRange(khaiThacBiz.GetAllMaTaiLieu());

            data.Sort();
            data.Insert(0, string.Empty);

            drlMaTaiLieu.DataSource = data;
        }

        private void LoadDataForNguoiNhan()
        {
            List<string> data = new List<string>();

            var khaiThacBiz = new PhieuBanGiaoTaiLieuBiz();
            data.AddRange(khaiThacBiz.GetAllNguoiNhan());
            data.Sort();
            data.Insert(0, string.Empty);

            drlNguoiNhan.DataSource = data;
        }

        private void LoadDataForDonVi()
        {
            List<string> data = new List<string>();

            var khaiThacBiz = new PhieuBanGiaoTaiLieuBiz();
            data.AddRange(khaiThacBiz.GetAllDonVi());
            data.Sort();
            data.Insert(0, string.Empty);

            drlDonVi.DataSource = data;
        }

        private void LoadDataForNam()
        {
            List<string> data = new List<string>();

            var khaiThacBiz = new PhieuBanGiaoTaiLieuBiz();
            data.AddRange(khaiThacBiz.GetAllNam());
            data.Sort();
            data.Insert(0, string.Empty);

            drlNam.DataSource = data;
        }

        private void Search(object sender, EventArgs e)
        {
            BuildData();            
        }
    }
}
